package com.sky.wsp.cas.client.config;

import org.jasig.cas.client.session.SingleSignOutFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.cas.web.CasAuthenticationFilter;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutFilter;

@EnableWebSecurity(debug = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 
    @Autowired
    private AuthenticationProvider authenticationProvider;
 
    @Autowired
    private AuthenticationEntryPoint entryPoint;
   
    @Autowired
    private CasAuthenticationFilter casAuthenticationFilter;
 
    @Autowired
    private SingleSignOutFilter singleSignOutFilter;
 
    @Autowired
    private LogoutFilter requestSingleLogoutFilter;
 
    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
 
        auth.authenticationProvider(authenticationProvider);
    }
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/**")
                .hasRole("ADMIN")
                .antMatchers("/user/**")
                .hasRole("USER")
                .antMatchers("/login/cas", "/favicon.ico", "/error")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .exceptionHandling()
                //设置认证入口
                .authenticationEntryPoint(entryPoint)
                .and()
                //添加过滤器,执行单点登录处理逻辑
                .addFilter(casAuthenticationFilter)
                //处理认证逻辑
                .addFilterBefore(singleSignOutFilter, CasAuthenticationFilter.class)
                //处理退出登录
                .addFilterBefore(requestSingleLogoutFilter, LogoutFilter.class);
    }
 
}